/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;


import controler.NodeItemControler;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.TransferHandler;
/**
 *
 * @author Tomek
 */
public class NodeItem extends JLabel {
    private Point begin;
    private Point end;
    private NodeItem instance;
    private boolean dragging=false;
    private Component selectedComponent;
    private Point offset;
    private NodeItemControler nodeItemControler;
    private Color toolboxcolor=Color.black;

    public NodeItem(NodeItemControler nic){
       
       setSize(75, 100);
       setText(" Punkt");
       setTransferHandler(new TransferHandler("text"));
       addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e) {
               System.out.println("mouseClicked");
            }
            public void mousePressed(MouseEvent e) {
                nodeItemControler.redNode();
                JComponent c = (JComponent) e.getSource();
                TransferHandler handler = c.getTransferHandler();
                handler.exportAsDrag(c, e, TransferHandler.COPY);
 
            }

            public void mouseReleased(MouseEvent e) {
            }
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
        });
        instance = this;
        nodeItemControler = nic;
        setToolTipText("Przeciągnij, aby dodać.");
    }

    public void setToolboxColor(Color toolboxcolor){
        this.toolboxcolor=toolboxcolor;
    }

    public Color getToolboxColor(){
        return toolboxcolor;
    }


    private void initWindow(){

    }

    public NodeItemControler getControler(){
        return nodeItemControler;
    }
  
    @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        if (instance.getText().compareTo(" Punkt")==0)
            g2.setColor(toolboxcolor);
        else{
        g2.setColor(getColor());
        nodeItemControler.setLabel();
        }
        System.out.println("pozycja"+getX()+" "+getY());
        g2.fillOval(0,0, 40, 40);
        if(nodeItemControler.showIcons()==true){
        if (nodeItemControler.getModel().isLock()==true)
            g2.drawImage(nodeItemControler.getBlockImage(), null, 9, 9);}
    }

    public void paintToFile(Graphics g){
        
        Graphics2D g2 = (Graphics2D)g;
        if (instance.getText().compareTo(" Punkt")==0)
            g2.setColor(toolboxcolor);
        else{
            g2.setColor(getColor());
            nodeItemControler.setLabel();
        }
        System.out.println("pozycja"+getX()+" "+getY());
        g2.fillOval(getX(),getY(), 40, 40);
        String name = nodeItemControler.getModel().getName();
        g2.drawString(name, getX()-5, getY()+50);
        if(nodeItemControler.showIcons()==true){
        if (nodeItemControler.getModel().isLock()==true)
            g2.drawImage(nodeItemControler.getBlockImage(), null, 9, 9);}
    }

  

    public void addMouseMotions(){
        addMouseMotionListener(new MouseMotion());
        addMouseListener(new MyMouseListener());
    }

    public NodeItemControler getNodeItemControler(){
        return nodeItemControler;
    }


    private class MouseMotion implements MouseMotionListener{

        public void mouseDragged(MouseEvent e) {

           
             if(dragging)
            {
                Rectangle r = selectedComponent.getBounds();
                r.x += e.getX() - offset.x;
                r.y += e.getY() - offset.y;
                selectedComponent.setBounds(r);
                nodeItemControler.refresh();
                
                nodeItemControler.setX(r.x);
                nodeItemControler.setY(r.y);
               
                
            }
        }

        public void mouseMoved(MouseEvent e) {
          // System.out.println("movuje");
        }

       

    }

    public Color getColor(){
        return nodeItemControler.getModel().getColor();
    }

    public void setColor(Color color){
        nodeItemControler.getModel().setColor(color);
        repaint();
    }

    public Color getDefColor(){
        return nodeItemControler.getModel().getDefColor();
    }

    public void setDefColor(Color color){
        nodeItemControler.getModel().setDefColor(color);
        repaint();
    }
    private class MyMouseListener implements MouseListener{
        public void mousePressed(MouseEvent e)
        {
            selectedComponent = (Component)e.getSource();
            offset = e.getPoint();
            dragging = true;
 

        }

        public void mouseClicked(MouseEvent e) {
              
        }

        public void mouseReleased(MouseEvent e) {
           
        }

        public void mouseEntered(MouseEvent e) {
           
        }

        public void mouseExited(MouseEvent e) {
            
        }
    }
 
}
